﻿$(document).ready(function () {
    //Invoke AJAX method to get all the Approval Checks
    $.post(pathPrefix + "ApprovalChecks/GetApprovalCheckList", null, function (result) {
        $("#approvalCheckTemplate").tmpl(result).appendTo("#tblApprovalChecks");

        $(".IsSectionVal").each(function () {
            var isSection = $(this).val();
            $(this).siblings("[name='IsSection']").val(isSection);
        });
    }, "json");

    //when the user changes one of the input elements in a row
    $("tr :input").live("change", function () {
        var row = $(this).parents("tr");

        //get all the approval check data
        var data = row.find(":input").serializeObject();

        if (data.Title != "") {
            //Invoke AJAX method to update this Approval Check (insert or edit)
            $.post(pathPrefix + "ApprovalChecks/Update", data, function (result) {
                //update the ApprovalCheckId value (if inserted row)
                row.find("#ApprovalCheckId").val(result);
                row.find(".lnkDeleteApprovalCheck").show();

                row.find(".successCell img").fadeIn(1000).delay(2000).fadeOut();
            }, "json");
        }
    });

    $(".lnkDeleteApprovalCheck").live("click", function () {
        var row = $(this).parents("tr");
        var id = row.find("[name='ApprovalCheckId']").val();

        openConfirmDialog("האם אתה בטוח?", function () {
            //Invoke AJAX method to delete the Approval Check
            $.post(pathPrefix + "ApprovalChecks/Delete", { "id": id }, function (result) {
                row.remove();
            }, "json");
        });

        return false;
    });

    //when the user clicks "AddApprovalCheck"
    $("#btnAddApprovalCheck").click(function () {
        //add new row using the template
        var row = $("#approvalCheckTemplate").tmpl(null).appendTo("#tblApprovalChecks");

        row.find(":input").val("");

        row.find(".lnkDeleteApprovalCheck").hide();
    });
});